package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Exam;
import cn.wolfcode.domain.ExamQuestion;
import cn.wolfcode.domain.Question;
import cn.wolfcode.mapper.ExamQuestionMapper;
import cn.wolfcode.qo.NewExam;
import cn.wolfcode.service.IExamService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IQuestionService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;

import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/exam")
public class ExamController {

    @Autowired
    private IExamService examService;
    @Autowired
    private IQuestionService questionService;


    @RequiredPermission(name="试卷页面",expression="exam:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Exam> pageInfo = examService.query(qo);
        model.addAttribute("pageInfo", pageInfo);

        //单选题
        List<HashMap> list = questionService.selectSingleQuestions(1L, 1L);
        model.addAttribute("list",list);

        //单选题总分
        int totalScore = 0;
        for (HashMap hashMap : list) {
            totalScore = totalScore + (int)hashMap.get("题目分数");
        }
        model.addAttribute("totalScore",totalScore);

        //多选题
        List<HashMap> list2 = questionService.selectMultipleQuestions(1L, 2L);
        model.addAttribute("list2",list2);

        //多选题总分
        int totalScore2 = 0;
        for (HashMap hashMap : list2) {
            totalScore2 = totalScore2 + (int)hashMap.get("题目分数");
        }
        model.addAttribute("totalScore2",totalScore2);

        //判断题
        List<HashMap> list3 = questionService.selectJudgeQuestions(1L, 3L);
        model.addAttribute("list3",list3);

        //多选题总分
        int totalScore3 = 0;
        for (HashMap hashMap : list3) {
            totalScore3 = totalScore3 + (int)hashMap.get("题目分数");
        }
        model.addAttribute("totalScore3",totalScore3);

        return "/exam/list";
    }


    @RequiredPermission(name="试卷删除",expression="exam:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           examService.delete(id);
        }
        return "redirect:/exam/list";
    }


    @RequiredPermission(name="试卷新增/编辑",expression="exam:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(NewExam newExam) {
        //先生成试卷
        examService.save(newExam.getExam());
        //System.out.println(exam.getId());
        questionService.addQuestions(newExam);
        return "redirect:/exam/list";
    }


    @RequiredPermission(name="试卷新增/编辑",expression="exam:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("exam",examService.get(id));
        }
        return "exam/input";
    }
}
